<template>
  <div class="body" v-loading="loading">
    <div>
      <el-button type="info" size="small" icon="el-icon-back" @click="gotoList">&nbsp;返&nbsp;回&nbsp;</el-button>
    </div>
    <el-form ref="form" :model="header" size="small" label-width="80px" :inline="true" class="header">
      <div class="audit_icon iconfont" v-if="header.status === 'audit'"></div>
      <el-row>
        <el-col :span="6">
          <el-form-item label="物料名称">{{ header.materialName }}</el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="物料编码">{{ header.materialNo }}</el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="物料规格">{{ header.materialSpec }}</el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="仓库名称">{{ getStockName(header.stockNo) }}</el-form-item>
        </el-col>
      </el-row>
      <el-row>
        <el-col :span="6">
          <el-form-item label="批次号">{{ header.batchNo }}</el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="箱数">{{ header.boxCount }}</el-form-item>
        </el-col>
        <el-col :span="6">
          <el-form-item label="重量">{{ header.count ? Number(header.count.toFixed(4)) : '' }}{{
              header.unit
            }}
          </el-form-item>
        </el-col>
      </el-row>
      <br/>
    </el-form>
    <el-table :data="dataList" class="detail" height="calc(100vh - 250px)">
      <el-table-column label="No." width="60" align="right" type="index"></el-table-column>
      <el-table-column label="箱号" width="200" prop="boxNo"></el-table-column>
      <el-table-column label="状态" prop="status" width="70">
        <template #default="scope">
          <el-tag :type="scope.row.status === 'in'? 'success': 'warning'">{{
              scope.row.status === 'in' ? '在库' : '出库'
            }}
          </el-tag>
        </template>
      </el-table-column>
      <el-table-column label="仓位" width="70" prop="locationNo">
        <template #default="scope">
          {{ getLocationName(scope.row.locationNo) }}
        </template>
      </el-table-column>
      <el-table-column label="重量" width="80"  align="right">
        <template #default="scope">
          {{ scope.row.count.toFixed(2) }}
        </template>
      </el-table-column>
      <el-table-column label="rfid" prop="rfid"></el-table-column>
      <el-table-column label="托盘编号" prop="trayNumber"></el-table-column>
      <el-table-column label="追溯码" min-width="160" prop="traceBack">
        <template #default="scope">
          <div v-for="item in scope.row.traceBack" style="display: flex">
            <div style="margin-right: 5px">{{ getTraceBackNo(item.traceBackNo) }}</div>
            <div>
              <el-tag size="mini" :type="item.status === 'in'? 'success': 'warning'">
                {{ item.status === 'in' ? '在库' : '出库' }}
            </el-tag>
            </div>
          </div>
        </template>
      </el-table-column>
<!--      <el-table-column label="追溯重量" width="100" >-->
<!--        <template #default="scope">-->
<!--          {{ scope.row.traceCount? Number(scope.row.count.toFixed(2)): scope.row.count }}-->
<!--        </template>-->
<!--      </el-table-column>-->
      <el-table-column label="成品箱号" min-width="100" width="140" prop="heatBoxNo"></el-table-column>
      <el-table-column label="成品箱数合计" width="120" prop="heatBoxNoCount" align="right"></el-table-column>
      <el-table-column label="原箱号" width="200" prop="oldBoxNo"></el-table-column>
    </el-table>
  </div>
</template>

<script>
import dayjs from 'dayjs'
import {getBoxNo} from '../../utils'

export default {
  data() {
    return {
      loading: false,
      materialLoading: false,
      materialList: [],
      supplierLoading: false,
      supplierList: [],
      header: {},
      dataList: [],
      detailBoxList: [],
      selection: [],
      stockList: [],
      locationList: [],
    }
  },
  async mounted() {
    await this.getStock()
    let id = this.$route.query.id
    if (id) {
      return this.getBill(id)
    }
  },
  methods: {
    getTraceBackNo(traceBackNo) {
      return traceBackNo.substring(traceBackNo.indexOf('=') + 1);
    },
    subStr(str) {
      if (!str) return ""
      return str.substring(str.indexOf('=') + 1)
    },
    async getStock() {
      this.loading = true;
      try {
        let res = await this.$api.getChildDictionaryListByKeys({
          keys: ['stock', 'location']
        });
        if (!res.res) return;
        this.stockList = res.stock;
        this.locationList = res.stockDetail;
      } finally {
        this.loading = false;
      }
    },
    /**
     * 获取仓库名
     */
    getStockName(val) {
      if (this.stockList && this.stockList.length > 0) {
        for (let i = 0; i < this.stockList.length; i++) {
          if (this.stockList[i].key === val) {
            return this.stockList[i].name;
          }
        }
      }
      return '';
    },
    /**
     * 获取仓位名
     */
    getLocationName(val) {
      if (this.locationList && this.locationList.length > 0) {
        for (let i = 0; i < this.locationList.length; i++) {
          if (this.locationList[i].key === val) {
            return this.locationList[i].name;
          }
        }
      }
      return '';
    },
    gotoList() {
      return this.$router.push('/inventory/inventoryList')
    },
    async getBill(id) {
      this.loading = true
      try {
        let res = await this.callApi('get_inventory_detail', {
          id: id,
        })
        if (res.res) {
          this.header = res.header || {}
          this.dataList = res.data || []
        }
      } finally {
        this.loading = false
      }
    },
    async getBox(id) {
      this.loading = true
      try {
        let res = await this.callApi('get_purin_bill_detail_box', {
          _detailId: id
        })
        if (res.res) {
          this.detailBoxList = res.data || []
          this.changeDetailCount()
        }
      } finally {
        this.loading = false
      }
    },
  }
}
</script>

<style lang="scss" scoped>
.body {
  padding: 20px 0;

  .header {
    margin-top: 20px;
    position: relative;

    .header_row {
      display: flex;

      > div {
        flex-shrink: 0;
      }

      :deep(.produceDate) {
        .el-input {
          width: 100% !important;
        }
      }
    }

    .audit_icon {
      position: absolute;
      top: -10px;
      left: 60px;
      font-size: 48px;
      color: red;
    }

    .material {
      width: 100%;
    }

    .p3 {
      :deep(.el-form-item__content) {
        display: flex;

        .comment {
          margin-left: 10px;
          word-break: keep-all;
        }
      }
    }
  }

  .detail_header {
    display: flex;
    justify-content: space-between;
    margin-top: 10px;

    .stat {
      display: flex;
      font-size: 15px;

      > div {
        display: flex;
        align-items: baseline;
        line-height: 32px;

        .label {
          text-align: right;
          margin-right: 5px;
          color: #aaa;
        }

        .value {
          min-width: 50px;
          margin-right: 10px;
          color: #666;
        }
      }
    }
  }
}
</style>
